package com.jwsoft.manager.core.gateway.controller;

import com.alibaba.fastjson.JSON;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.cache.redis.RedisHelper;
import com.bifang.engine.protect.core.annotation.NoSignCrypto;
import com.bifang.module.base.common.vo.dict.SelectVO;
import com.bifang.module.base.common.vo.excel.ExcelVO;
import com.bifang.module.base.core.utils.ExcelFileUtil;
import com.bifang.module.base.core.utils.ServiceRequestUtil;
import com.jwsoft.manager.common.constant.EduCache;
import com.jwsoft.manager.common.vo.eduSyncJhApply.EduSyncJhApplyQueryVO;
import com.jwsoft.manager.common.vo.eduSyncJhApply.EduSyncJhApplyVO;
import com.jwsoft.manager.core.integration.EduSyncJhApplyIntegration;
import com.jwsoft.manager.core.integration.EduSyncJhApproveIntegration;
import com.jwsoft.manager.common.vo.eduSyncJhApprove.EduSyncJhApproveVO;
import com.jwsoft.manager.common.vo.eduSyncJhApprove.EduSyncJhApproveKeyVO;
import com.jwsoft.manager.common.vo.eduSyncJhApprove.EduSyncJhApproveQueryVO;
import com.github.pagehelper.PageInfo;
import com.bifang.engine.assistant.core.domain.AppResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import com.bifang.engine.protect.core.annotation.Idempotent;
import org.springframework.validation.annotation.Validated;
import com.bifang.assistant.common.constants.Validators;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * 金华市审核专库同步情况表网关
 *
 * @author zhiyuan
 * @since 2024-04-08
 */
@RestController
@RequestMapping(value = "/doc/api")
@Tag(name = "金华市审核专库同步情况表", description = "金华市审核专库同步情况表")
public class EduSyncJhApproveController {

    @Autowired
    public EduSyncJhApproveIntegration eduSyncJhApproveIntegration;

    @Autowired
    public RedisHelper redisHelper;

    @Operation(summary = "金华市审核专库同步情况表分页查询",description = "金华市审核专库同步情况表分页查询")
    @PostMapping(value = "/syncJhApprove0001", produces = "application/json;charset=UTF-8")
    public AppResponse<PageInfo<EduSyncJhApproveVO>> getList(@RequestBody @Validated EduSyncJhApproveQueryVO vo) {
        return AppResponse.successData(eduSyncJhApproveIntegration.getList(vo));
    }

    @Operation(summary = "金华市审核专库同步情况表根据主键查询详情",description = "金华市审核专库同步情况表根据主键查询详情")
    @PostMapping(value = "/syncJhApprove0002", produces = "application/json;charset=UTF-8")
    public AppResponse<EduSyncJhApproveVO> getById(@RequestBody @Validated EduSyncJhApproveKeyVO vo) {
        return AppResponse.successData(eduSyncJhApproveIntegration.getById(vo));
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "金华市审核专库同步情况表新增",description = "金华市审核专库同步情况表新增")
    @PostMapping(value = "/syncJhApprove0004", produces = "application/json;charset=UTF-8")
    public AppResponse<EduSyncJhApproveVO> add(@RequestBody @Validated(value = {Validators.Add.class}) EduSyncJhApproveVO vo) {
        return AppResponse.successData(eduSyncJhApproveIntegration.add(vo));
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "金华市审核专库同步情况表修改",description = "金华市审核专库同步情况表修改")
    @PostMapping(value = "/syncJhApprove0005", produces = "application/json;charset=UTF-8")
    public AppResponse<EduSyncJhApproveVO> edit(@RequestBody @Validated(value = {Validators.Edit.class}) EduSyncJhApproveVO vo) {
        return AppResponse.successData(eduSyncJhApproveIntegration.edit(vo));
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "金华市审核专库同步情况表删除",description = "金华市审核专库同步情况表删除")
    @PostMapping(value = "/syncJhApprove0006", produces = "application/json;charset=UTF-8")
    public AppResponse del(@RequestBody @Validated EduSyncJhApproveKeyVO vo) {
        eduSyncJhApproveIntegration.del(vo);
        return AppResponse.successMsg("删除成功！");
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "金华市审核专库同步情况-excel导出",description = "金华市审核专库同步情况-excel导出")
    @PostMapping(value = "/syncJhApprove0007", produces = "application/json;charset=UTF-8")
    @NoSignCrypto
    public void exportJhApproveInfo(@RequestBody @Validated EduSyncJhApproveQueryVO vo, HttpServletResponse response) {
        eduSyncJhApproveIntegration.exportJhApproveInfo(vo,response);
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "金华市审核专库同步情况-excel导入并保存",description = "金华市审核专库同步情况-excel导入并保存")
    @PostMapping(value = "/syncJhApprove0008", produces = "application/json;charset=UTF-8")
    @NoSignCrypto
    public AppResponse inputAndSaveJhApproveInfo(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            HttpServletResponse response
    ) throws IOException {
        ExcelVO vo= ExcelFileUtil.getExcelVO(request,response,paramMap);
        MultipartFile file=ExcelFileUtil.getExcelFile(request);
        eduSyncJhApproveIntegration.inputAndSaveJhApproveInfo(vo,file);
        return AppResponse.successMsg("导入成功！");
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "审核同步模板下载（PC）",description = "审核同步模板下载（PC）")
    @PostMapping(value = "/syncJhApprove0009", produces = "application/json;charset=UTF-8")
    @NoSignCrypto
    public void downloadByCode(
            HttpServletRequest request,
            @RequestParam Map<String, Object> paramMap,
            @RequestBody(required = false) String body,
            HttpServletResponse response)  {
        ExcelVO vo = null;
        if (StringUtils.isNotBlank(body)) {
            vo = JSON.parseObject(body, ExcelVO.class);
        } else {
            vo = new ExcelVO();
            if (paramMap.get("modelCode") != null) {
                vo.setModelCode(paramMap.get("modelCode").toString());
            }
            if (paramMap.get("excelVersion") != null) {
                vo.setExcelVersion(paramMap.get("excelVersion").toString());
            }
            if (paramMap.get("rows") != null) {
                vo.setRows(Integer.parseInt(paramMap.get("rows").toString()));
            }
            if (paramMap.get("dictMap") != null) {
                vo.setDictMap(
                        (Map<String, List<SelectVO>>)
                                JSON.parse(JSON.toJSONString(paramMap.get("dictMap"))));
            }
        }
        vo.setAccessToken(ServiceRequestUtil.getAccessToken(request, response));
        if (StringUtils.isBlank(vo.getModelCode())) {
            throw new AppException("excel模板编号不能为空");
        }
        eduSyncJhApproveIntegration.downloadModel(vo, response);
    }

    @Idempotent(expire="PT2S") //2秒内防重复提交
    @Operation(summary = "审核同步导入锁根据行政区划清除",description = "审核同步导入锁根据行政区划清除")
    @PostMapping(value = "/syncJhApprove0010", produces = "application/json;charset=UTF-8")
    @NoSignCrypto
    public AppResponse<EduSyncJhApplyVO> clearSyncJhApproveLock(@RequestBody EduSyncJhApplyVO vo) {
        if (StringUtils.isBlank(vo.getApplyCountyCode())){
            throw new AppException("报名所在区县码不能为空");
        }
        String redisKey= EduCache.EdU_SYNC_JH_APPROVE_LOCK+vo.getApplyCountyCode();
        redisHelper.del(redisKey);
        return AppResponse.successMsg("解锁成功！");
    }
}
